package com.hangzhou.xyj.ui.fragment;

import android.app.Dialog;
import android.os.AsyncTask;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.DianDramaDao;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.DianDramaModel;
import com.hangzhou.xyj.ui.adapter.AlreadyDianAdapter;
import com.hangzhou.xyj.ui.fragment.base.BaseFragment;
import com.hangzhou.xyj.ui.listener.RecycleOnItemClickListener;
import com.hangzhou.xyj.ui.widget.SelectMenuDialog;
import com.hangzhou.xyj.utils.ToastUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页唱戏 已点戏曲
 *
 * @author neo.duan
 */
public class AlreadyDianFragment extends BaseFragment {

    private RecyclerView mRecyclerView;
    private AlreadyDianAdapter mAdapter;

    @Override
    public void initTop() {
        enableTop(false);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.layout_common_recycleview);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_common);
        //设置水平布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置水平分割线
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST
//                , R.drawable.line_divider_one));
        mRecyclerView.setAdapter(mAdapter = new AlreadyDianAdapter(mContext, null));
    }

    @Override
    public void initDatas() {
        //向服务器请求数据
//        MyOrderedSingReq myOrderedSingReq = new MyOrderedSingReq(1, 1, 10);//TODO 要改的
//        HttpLoader.getInstance().sendRequest(new MyOrderedSingHandler(myOrderedSingReq, new MyHttpResponseListener() {
//            @Override
//            public void onSuccess(Object model) {
//                super.onSuccess(model);
//                List<SingModel> data = ((SingPageInfoModel) model).getSingList();
//
//            }
//        }, 0));

    }

    @Override
    public void initListeners() {
        mAdapter.setOnItemClick(new RecycleOnItemClickListener<DianDramaModel>() {
            @Override
            public void onItemClick(View view, DianDramaModel model) {
                //弹出操作菜单对话框
                if (model != null) {
                    showMenuDialog(model);
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }

    private void loadData() {
        new AsyncTask<Void, Void, List>() {

            @Override
            protected List<DianDramaModel> doInBackground(Void... params) {
                return getDianDramaData();
            }

            @Override
            protected void onPostExecute(List list) {
                super.onPostExecute(list);
                mAdapter.update(list);
            }
        }.execute();
    }

    /**
     * 弹出操作对话框
     */
    private void showMenuDialog(final DianDramaModel model) {
        SelectMenuDialog dialog = new SelectMenuDialog(mContext);
        dialog.setMenu(new String[]{
                "我要唱", "删除"
        }, new SelectMenuDialog.SelectMenuClickListener() {
            //按钮添加点击事件
            @Override
            public void onSelectMenuClick(Dialog dialog, View item, int position) {
                switch (position) {
                    case 0:
                        IntentManager.getInstance().goRecordActivity(mContext, model);
                        break;
                    case 1:
                        deleteDrama(model);
                        //重新load数据
                        loadData();
                        break;
                }
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    /**
     * 从数据库获取录音信息
     */
    private List<DianDramaModel> getDianDramaData() {
        DianDramaDao dao = new DianDramaDao(mContext);
        List<DianDramaModel> dramas = new ArrayList<>();
        if (XYJApplication.getUserInfo() != null)
            dramas = dao.getDramasByUser(XYJApplication.getUserInfo().getUserId());
        return dramas;
    }

    /**
     * 从数据库中删除该录制
     *
     * @param model
     */
    private void deleteDrama(DianDramaModel model) {
        DianDramaDao dao = new DianDramaDao(mContext);
        try {
            dao.delete(model);
        } catch (SQLException e) {
            e.printStackTrace();
            ToastUtil.show("删除失败");
        }
    }
}
